<?php
/**
 * Created by PhpStorm.
 * Script Name: HandlerPrivateChat.php
 * Create: 2022/7/18 13:57
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\bot\handler;

use app\common\service\Tenant as TenantService;
use plugin\wechat\app\service\BotReply as ReplyService;
use plugin\wechat\app\service\BotKeyword as KeywordService;
use plugin\wechat\app\service\Bot as BotService;
use plugin\wechat\app\service\BotTask as TaskService;

class HandlerGroupChat extends Handler
{
    /**
     * 针对消息事件的特殊响应
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function eventReply(){
        $request = \request();
        $replys = ReplyService::getReplies([
                ['bot_id','=', $request->bot['id']],
                ['event','=', ReplyService::MSG],
                ['status' , '=', 1],
                ['msg_type','=', $request->content['type']]
        ]);

        foreach ($replys as $k => $reply){
            if(empty($reply['wxids']) || strpos($reply['wxids'], $request->groupWxid) !== false){
                switch ($reply['handle_type']){
                    case ReplyService::HANDLE_RM:
                        $request->botClient->removeGroupMember(['robot_wxid' => $request->botWxid, 'group_wxid' => $request->groupWxid, 'to_wxid' => $request->fromWxid]);
                        break;
                    default:
                        ReplyService::botReply($request->bot, $request->botClient, $reply, $request->groupWxid);
                        break;
                }
            }
        }
    }

    /**
     * 插件处理
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function addon(){
        $apps = TenantService::getCompanyActiveApps(TenantService::getCompanyById($this->bot['company_id']), [['name','<>', 'wechat']]);
        foreach ($apps as $k => $v){
            $class_name = '\\plugin\\'.$v['name'].'\\app\\platform\\controller\\Bot';
            if(class_exists($class_name)){
                $class = new $class_name();
                if(method_exists($class, 'groupChatHandle')){
                    $class->groupChatHandle();
                }
            }
        }
    }

    /**
     * todo 移出群
     *
     * 1.根据昵称找到wxid
     * 2.过滤白名单
     * 3.判断是否达到群规设置的次数
     *
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function rmGroupMember(){

    }

    /**
     * 关键词回复
     * @return bool
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function keyword(){
        $request = \request();
        $keywords = KeywordService::getKeywordsByBot([
            'bot_id' => $request->bot['id'],
            'keyword' => $request->content['msg'],
        ]);

        $flag = false;
        foreach ($keywords as $keyword){
            if(empty($keyword['wxids'])){
                $where = [
                    ['uin' ,'=' ,$request->botWxid]
                ];
                if($keyword['user_type'] == TaskService::USER_TYPE_FRIEND){
                    $where[] = ['type', '=', BotService::FRIEND];
                }elseif($keyword['user_type']==TaskService::USER_TYPE_GROUP){
                    $where[] = ['type', '=', BotService::GROUP];
                }
                $keyword['wxids'] = implode(',', $this->memberM->where($where)->column('wxid'));
            }
            if(strpos($keyword['wxids'], $request->groupWxid) !== false){
                ReplyService::botReply($request->bot, $request->botClient, $keyword, $request->groupWxid,
                    ['nickname' => $request->content['from_name'], 'need_at' => $keyword['need_at'], 'member_wxid' => $request->fromWxid]
                );
                $flag = true;
            }
        }

        return $flag;
    }
}